package com.exmple.testespresso

import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import androidx.test.core.app.ApplicationProvider.getApplicationContext
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SdkSuppress
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import androidx.test.uiautomator.By
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.Until
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.CoreMatchers.notNullValue
import org.hamcrest.core.Is
import org.junit.Assert.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith


/*
    UI Automator
    https://developer.android.google.cn/training/testing/ui-automator

    示例
    https://github.com/android/testing-samples/blob/master/ui/uiautomator/BasicSample/app/src/androidTest/java/com/example/android/testing/uiautomator/BasicSample/ChangeTextBehaviorTest.java


    测试多个应用的界面
    https://developer.android.google.cn/training/testing/ui-testing/uiautomator-testing

    AndroidSDK 自带定位工具 uiautomatorviewer
    http://t.zoukankan.com/guo2733-p-10556791.html

    uiautomatorviewer无法启动: 原因：java版本过高
    https://blog.csdn.net/dorisx/article/details/124021082

    关于 android 工具 uiautomatorviewer.bat 无法正常启动的问题
    https://blog.csdn.net/A951860555/article/details/121046647

    uiautomatorviewer 启动出错
    https://juejin.cn/post/7012625112448892941
 */

@RunWith(AndroidJUnit4::class)
@SdkSuppress(minSdkVersion = 18)
class MainActivityUiautomatorTest {
    companion object{
        const val BASIC_SAMPLE_PACKAGE = "com.exmple.testespresso"
        const val LAUNCH_TIMEOUT:Long = 5000
        const val STRING_TO_BE_TYPED = "6660001000"
    }

    private lateinit var mDevice : UiDevice
    @Before
     fun startMainActivityFromHomeScreen() {
        mDevice = UiDevice.getInstance(getInstrumentation())//注意 getInstrumentation 导包
        mDevice.pressHome()//模拟按下“主屏幕”按钮


        // Wait for launcher
        val launcherPackage = getLauncherPackageName()
        assertThat(launcherPackage, notNullValue())
        mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT)

        // Launch the blueprint app
        val context = getApplicationContext<Context>()
        val intent = context.packageManager.getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE)
        intent!!.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK) // Clear out any previous instances
        context.startActivity(intent)


        // Wait for the app to appear
        mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)), LAUNCH_TIMEOUT)
    }
    @Test
    fun checkPreconditions() {
        assertThat(mDevice, notNullValue());
    }
    @Test
    fun testChangeText_MainActivity() {
        // Type text and then press the button.
        mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "login_et_moiblie_no")).setText(STRING_TO_BE_TYPED)
        mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "tv_click")).click()

        // Verify the test is displayed in the Ui
        val changedText = mDevice.wait(Until.findObject(By.res(BASIC_SAMPLE_PACKAGE, "tv_text")), 500 /* wait 500ms */)
        assertThat(changedText.text, Is(equalTo(STRING_TO_BE_TYPED)))
    }
    /**
     * Uses package manager to find the package name of the device launcher. Usually this package
     * is "com.android.launcher" but can be different at times. This is a generic solution which
     * works on all platforms.`
     */
    private fun getLauncherPackageName(): String? {
        // Create launcher Intent
        val intent = Intent(Intent.ACTION_MAIN)
        intent.addCategory(Intent.CATEGORY_HOME)

        // Use PackageManager to get the launcher package name
        val pm = getApplicationContext<Context>().packageManager
        val resolveInfo = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY)
        return resolveInfo!!.activityInfo.packageName
    }
}